package com.wzx.ptplatform.controller.admin.university;

import com.wzx.ptplatform.domain.Major;
import com.wzx.ptplatform.service.MajorService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员学校模块——学校专业管理")
@RestController
public class AdminUniversityMajorController {

    final MajorService majorService;

    public AdminUniversityMajorController(MajorService majorService) {
        this.majorService = majorService;
    }

    @RequestMapping("admin_university_major_message_list")
    public LayuiPager<Major> adminUniversityMajorMessageList(
            String majorName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Major major = new Major();
        if (majorName != null && !"".equals(majorName)) {
            major.setMajorName(majorName);
        }
        Page<Major> pager = majorService.findList(major, page - 1, limit);
        LayuiPager<Major> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_university_major_message_addView")
    public ModelAndView adminUniversityMajorMessageAddView() {
        return new ModelAndView("admin/university/major_message_add");
    }

    @GetMapping("admin_university_major_message_editView")
    public ModelAndView adminUniversityMajorMessageEditView(String id, Model model) {
        model.addAttribute("major", majorService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/university/major_message_edit");
    }

    @PostMapping("admin_university_major_message_save_or_edit")
    public Map<String, Object> adminUniversityMajorMessageSaveOrEdit(Major major) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (majorService.saveOrEdit(major)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_university_major_message_delete")
    public Map<String, Object> adminUniversityMajorMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (majorService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_university_major_message_delete_select")
    public Map<String, Object> adminUniversityMajorMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    majorService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
